/*
Mau Update secara Otomatis?
Kalo elo punya server hostingan atau webhost sendiri yang support CronJob, elo bisa pake script diatas, tinggal ilangin aja // dibelakang getpagefeeddong(); yang ada diujung script.

Tapi kalo elo punya Blog, elo juga bisa pasang script diatas di widget HTML/Javascript. Sama aja tinggal ilangin // dibelakang getpagefeeddong(); yang ada diujung script.
*/
// Ini udah Auto Update.
var AccessToken_UpStat = 'TOKEN_USER';
var UserId_ygMau_UpStat = 'ID_USER';
var PageId_bwt_UpStat = 'ID_PAGES';
var markpost = 'no';
if (!document.getElementById('laporan-hasil-update-elo')) {
  document.write('<div id="laporan-hasil-update-elo" style="white-space:pre-wrap"><a href="javascript:void(0)" onClick="getpagefeeddong()">Click here to start</a></div>')
}
function tarosApibos(uri) {
  var e = document.createElement('script');
  e.async = true;
  e.src = uri;
  document.getElementsByTagName('head')[0].appendChild(e);
}
function getpagefeeddong() {
  document.getElementById('laporan-hasil-update-elo')
    .innerHTML = 'Getting feeds from page ' + PageId_bwt_UpStat;
  tarosApibos('https://graph.facebook.com/' + PageId_bwt_UpStat + '/posts?fields=message,from&access_token=' + AccessToken_UpStat + '&callback=hasilgetpagefeeddong');
}
function hasilgetpagefeeddong(post) {
  if (post.error) {
    document.getElementById('laporan-hasil-update-elo')
      .innerHTML = JSON.stringify(post, null, '\t')
  }
  if (post.data && post.data.length != 0) {
    post.data = post.data.sort(function () {
      return 0.5 - Math.random()
    });
    for (x in post.data) {
      if (post.data[x].message && post.data[x].from && post.data[x].from.name) {
        idtopost = post.data[x].id.split('_')[1];
        urltopost = 'https://graph.facebook.com/' + UserId_ygMau_UpStat + '/feed?method=POST&message=' + escape(post.data[x].message) + '&access_token=' + AccessToken_UpStat + '&callback=hasilpostfeeddong';
        document.getElementById('laporan-hasil-update-elo')
          .innerHTML = 'idtopost=' + idtopost + '<div>Getting feeds from user ' + UserId_ygMau_UpStat + '</div>';
        getusrfeeddong();
        break;
      }
    }
  }
}
function getusrfeeddong() {
  tarosApibos('https://graph.facebook.com/' + UserId_ygMau_UpStat + '/posts?fields=caption&access_token=' + AccessToken_UpStat + '&callback=hasilgetusrfeeddong');
}
function hasilgetusrfeeddong(post) {
  if (post.error) {
    document.getElementById('laporan-hasil-update-elo')
      .innerHTML = JSON.stringify(post, null, '\t')
  }
  if (post.data && post.data.length != 0) {
    for (x in post.data) {
      if (post.data[x].caption && post.data[x].caption == idtopost) {
        markpost = 'yes';
        document.getElementById('laporan-hasil-update-elo')
          .innerHTML = 'markpost=' + markpost + ' Status NOT Updated!';
        break;
      }
    }
  }
  if (markpost == 'no') {
    document.getElementById('laporan-hasil-update-elo')
      .innerHTML = 'Updating Status ' + UserId_ygMau_UpStat;
    tarosApibos(urltopost);
  }
}
function hasilpostfeeddong(post) {
  if (post.id) {
    document.getElementById('laporan-hasil-update-elo')
      .innerHTML = '<a href="http://www.facebook.com/' + post.id.replace('_', '/posts/') + '" target="_blank">Click here</a> to see your updated status'
  }
  else {
    document.getElementById('laporan-hasil-update-elo')
      .innerHTML = JSON.stringify(post, null, '\t')
  }
}
getpagefeeddong();